# 采集数据：Oozie调度MySQL到ODS
#!/bin/bash

# show partitions itcast_ods.web_chat_ems;
# show partitions itcast_ods.web_chat_text_ems;
# alter table itcast_ods.web_chat_ems drop partition (start_time='2020-12-28');
# alter table itcast_ods.web_chat_text_ems drop partition (start_time='2020-12-28');

# 需求：能够手动传入参数指定采集的日期。  如果不传入参数要求能够自动的识别前一天的日期
if [ $# -eq 0 ]
then
    DATESTR=`date -d "-1 day" +%Y-%m-%d`
else
    DATESTR=$1
fi
echo "${DATESTR}"
# Define var
SQOOP_HOME=/usr/bin/sqoop
JDBCSTR="jdbc:mysql://192.168.10.150:3306/nev"
MYSQL_USERNAME=root
MYSQL_PASSWORD=123456
YEARSTR=`date -d "-1 day" +%Y`
MONTHSTR=`date -d "-1 day" +%m`
HIVE_DB=itcast_ods
MAP_NUMBER=3


echo "执行采集web_chat_ems_${YEARSTR}_${MONTHSTR}的任务......"

$SQOOP_HOME import \
--connect $JDBCSTR \
--username $MYSQL_USERNAME --password $MYSQL_PASSWORD \
--query "SELECT
  id,create_date_time,session_id,sid,create_time,
  seo_source,seo_keywords,ip,area,country,province,
  city,origin_channel,user AS user_match,manual_time,
  begin_time,end_time,last_customer_msg_time_stamp,
  last_agent_msg_time_stamp,reply_msg_count,msg_count,
  browser_name,os_info,'${DATESTR}' AS start_time
FROM web_chat_ems_${YEARSTR}_${MONTHSTR}
WHERE create_time between '${DATESTR} 00:00:00' and '${DATESTR} 23:59:59' and \$CONDITIONS" \
--fields-terminated-by '\t' \
--hcatalog-database $HIVE_DB \
--hcatalog-table web_chat_ems \
-m $MAP_NUMBER \
--split-by id

wait

echo "执行采集web_chat_text_ems_${YEARSTR}_${MONTHSTR}的任务......"
$SQOOP_HOME import \
--connect $JDBCSTR \
--username $MYSQL_USERNAME --password $MYSQL_PASSWORD \
--query "SELECT
id,referrer,from_url,landing_page_url,
url_title,platform_description,
other_params,history,'${DATESTR}' AS start_time
FROM web_chat_text_ems_2019_07
WHERE 1=1 AND \$CONDITIONS" \
--fields-terminated-by '\t' \
--hcatalog-database $HIVE_DB \
--hcatalog-table web_chat_text_ems \
-m $MAP_NUMBER \
--split-by id